﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.OracleClient;

using Maticsoft.DBUtility;
using DQYT.HTDZ.Model;
using System.Data;
using DQYT.HTDZ.IDAL;


namespace DQYT.HTDZ.OracleDAL
{
    public class BDDepartmentDAL : IBDDepartment
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BUM_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_sys_department");
            strSql.Append(" where BUM_ID=:BUM_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":BUM_ID", OracleType.Number,4)			};
            parameters[0].Value = BUM_ID;

            return DbHelperOra.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BDDepartmentModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_sys_department(");
            strSql.Append("BUM_ID,BUM_MC,BUM_MS,BUM_FZR,BUM_DH,BUM_BZ,BUM_SJID,BUM_RS)");
            strSql.Append(" values (");
            strSql.Append(":BUM_ID,:BUM_MC,:BUM_MS,:BUM_FZR,:BUM_DH,:BUM_BZ,:BUM_SJID,:BUM_RS)");
            OracleParameter[] parameters = {
					new OracleParameter(":BUM_ID", OracleType.Int32),
					new OracleParameter(":BUM_MC", OracleType.NVarChar),
					new OracleParameter(":BUM_MS", OracleType.NVarChar),
					new OracleParameter(":BUM_FZR", OracleType.NVarChar),
					new OracleParameter(":BUM_DH", OracleType.NVarChar),
					new OracleParameter(":BUM_BZ", OracleType.NVarChar),
					new OracleParameter(":BUM_SJID", OracleType.Int32),
					new OracleParameter(":BUM_RS", OracleType.Int32)};
            parameters[0].Value = model.BUM_ID;
            parameters[1].Value = model.BUM_MC;
            parameters[2].Value = model.BUM_MS;
            parameters[3].Value = model.BUM_FZR;
            parameters[4].Value = model.BUM_DH;
            parameters[5].Value = model.BUM_BZ;
            parameters[6].Value = model.BUM_SJID;
            parameters[7].Value = model.BUM_RS;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BDDepartmentModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_sys_department set ");
            strSql.Append("BUM_MC=:BUM_MC,");
            strSql.Append("BUM_MS=:BUM_MS,");
            strSql.Append("BUM_FZR=:BUM_FZR,");
            strSql.Append("BUM_DH=:BUM_DH,");
            strSql.Append("BUM_BZ=:BUM_BZ,");
            strSql.Append("BUM_SJID=:BUM_SJID,");
            strSql.Append("BUM_RS=:BUM_RS");
            strSql.Append(" where BUM_ID=:BUM_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":BUM_MC", OracleType.NVarChar),
					new OracleParameter(":BUM_MS", OracleType.NVarChar),
					new OracleParameter(":BUM_FZR", OracleType.NVarChar),
					new OracleParameter(":BUM_DH", OracleType.NVarChar),
					new OracleParameter(":BUM_BZ", OracleType.NVarChar),
					new OracleParameter(":BUM_SJID", OracleType.Number,4),
					new OracleParameter(":BUM_RS", OracleType.Number,4),
					new OracleParameter(":BUM_ID", OracleType.Number,4)};
            parameters[0].Value = model.BUM_MC;
            parameters[1].Value = model.BUM_MS;
            parameters[2].Value = model.BUM_FZR;
            parameters[3].Value = model.BUM_DH;
            parameters[4].Value = model.BUM_BZ;
            parameters[5].Value = model.BUM_SJID;
            parameters[6].Value = model.BUM_RS;
            parameters[7].Value = model.BUM_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BUM_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_department ");
            strSql.Append(" where BUM_ID=:BUM_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":BUM_ID", OracleType.Number,4)			};
            parameters[0].Value = BUM_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string BUM_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_department ");
            strSql.Append(" where BUM_ID in (" + BUM_IDlist + ")  ");
            int rows = DbHelperOra.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BDDepartmentModel GetModel(int BUM_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BUM_ID,BUM_MC,BUM_MS,BUM_FZR,BUM_DH,BUM_BZ,BUM_SJID,BUM_RS from tb_sys_department ");
            strSql.Append(" where BUM_ID=:BUM_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":BUM_ID", OracleType.Number,4)			};
            parameters[0].Value = BUM_ID;

            BDDepartmentModel model = new BDDepartmentModel();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BUM_ID"] != null && ds.Tables[0].Rows[0]["BUM_ID"].ToString() != "")
                {
                    model.BUM_ID = int.Parse(ds.Tables[0].Rows[0]["BUM_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BUM_MC"] != null && ds.Tables[0].Rows[0]["BUM_MC"].ToString() != "")
                {
                    model.BUM_MC = ds.Tables[0].Rows[0]["BUM_MC"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_MS"] != null && ds.Tables[0].Rows[0]["BUM_MS"].ToString() != "")
                {
                    model.BUM_MS = ds.Tables[0].Rows[0]["BUM_MS"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_FZR"] != null && ds.Tables[0].Rows[0]["BUM_FZR"].ToString() != "")
                {
                    model.BUM_FZR = ds.Tables[0].Rows[0]["BUM_FZR"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_DH"] != null && ds.Tables[0].Rows[0]["BUM_DH"].ToString() != "")
                {
                    model.BUM_DH = ds.Tables[0].Rows[0]["BUM_DH"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_BZ"] != null && ds.Tables[0].Rows[0]["BUM_BZ"].ToString() != "")
                {
                    model.BUM_BZ = ds.Tables[0].Rows[0]["BUM_BZ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BUM_SJID"] != null && ds.Tables[0].Rows[0]["BUM_SJID"].ToString() != "")
                {
                    model.BUM_SJID = int.Parse(ds.Tables[0].Rows[0]["BUM_SJID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BUM_RS"] != null && ds.Tables[0].Rows[0]["BUM_RS"].ToString() != "")
                {
                    model.BUM_RS = int.Parse(ds.Tables[0].Rows[0]["BUM_RS"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BUM_ID,BUM_MC,BUM_MS,BUM_FZR,BUM_DH,BUM_BZ,BUM_SJID,BUM_RS ");
            strSql.Append(" FROM tb_sys_department ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_sys_department ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.BUM_ID desc");
            }
            strSql.Append(")AS Row, T.*  from tb_sys_department T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOra.Query(strSql.ToString());
        }

        #endregion  Method

        #region 新添加
        /// <summary>
        /// 获得部门人数
        /// </summary>
        /// <param name="BUM_ID">部门ID</param>
        /// <returns></returns>
        public int GetRenShu(int BUM_ID)
        {
            #region
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) FROM tb_sys_user WHERE BUM_ID IN");
            strSql.Append("(SELECT BUM_ID FROM tb_sys_department where BUM_ID=" + BUM_ID.ToString() + " OR (BUM_SJID=" + BUM_ID.ToString() + " AND BUM_SJID<>0 )) ");

            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;

            #endregion
        }

        /// <summary>
        /// 获得最大上级ID
        /// </summary>
        /// <returns></returns>
        public int GetMaxSJID()
        {
            return DbHelperOra.GetMaxID("BUM_SJID", "tb_sys_department");
        }
        /// <summary>
        /// 获得下一个ID
        /// </summary>
        /// <returns></returns>
        public int GetNextBUM_ID()
        {
            return DbHelperOra.GetMaxID("BUM_ID", "tb_sys_department");
        }
        /// <summary>
        /// 判断是否存在当前部门名称
        /// </summary>
        /// <param name="BUM_MC">部门名称</param>
        /// <returns></returns>
        public bool CheckMC(string BUM_MC)
        {
            #region
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_sys_department WHERE BUM_MC='" + BUM_MC + "'");

            object obj = DbHelperOra.GetSingle(strSql.ToString());
            int iNum = 0;
            if (obj != null)
            {
                iNum = Convert.ToInt32(obj);
            }
            if (iNum > 0)
            {
                return true;
            }
            return false;
            #endregion
        }
        #endregion
    }
}
